package com.ynhenc.pta.routeRank.impl;

import com.ynhenc.pta.routeRank.*;

/**
 * @author sbmoon
 * 
 */
public class SectionImpl extends ComLib_PTA implements Section {

	public double getQueryTime_Sec() {
		return queryTime_Sec;
	}

	public void setQueryTime_Sec(double queryTime_Sec) {
		this.queryTime_Sec = queryTime_Sec;
	}

	public SectionImpl(StartEndNode seNode, Path path) {
		this.seNode = seNode;
		this.path = path;
		this.debug("Section:" + seNode);
	}

	public SectionImpl( TransitType transitType, Node s, Node e, Path path) { 
		this( new StartEndNode( transitType, s, e ) , path );
	}

	public Node getNodeStart() {
		Path path = this.getPath();
		if (path == null) {
			return null;
		} else {
			return path.getNodeStart();
		}
	}

	public Node getNodeEnd() {
		Path path = this.getPath();
		if (path == null) {
			return null;
		} else {
			return path.getNodeEnd();
		}
	}

	public String getNameSection() {
		StartEndNode seNode = this.getStartEndNode();
		return this.getTransitType().getTransitName() + ", " 
		+ seNode.getStartNode().getName() 
		+ "~" + seNode.getEndNode().getName() 
		+ ", " + seNode.getDescription()
		;
	}
	
	public void showDebugMessage() {
		this.debug( this.getNameSection() + " " + this.getDescription() );
	}

	public String getDescription() {
		return ""
		+ "dist: " + this.getLength_Meter()/1000.0 + " (Km)"
		+ ", duration: " + this.getTime_Sec()/60.0 + " (min)"
		+ ", carbon: " + this.getCarbon_Gram() + " (g)"
		+ ", queryTime:" + this.getQueryTime_Sec() + " (sec)"
		 + ", " + this.getStartEndNode() 
		;
	}

	public void toKml(StringBuffer buff) {
		buff.append("<Placemark>");
		buff.append(NL);
		buff.append("<name>");
		buff.append(this.getNameSection());
		buff.append("</name>");
		buff.append(NL);
		buff.append("<description>");
		buff.append(this.getDescription());
		buff.append("</description>");
		buff.append(NL);
		buff.append("<styleUrl>#");
		buff.append(this.getTransitType().getKmlStyleName());
		buff.append("</styleUrl>");
		buff.append(NL);

		this.getPath().toKml(buff);

		buff.append(NL);
		buff.append("</Placemark>");
	}

	public TransitType getTransitType() {
		return this.getStartEndNode().getTransitType();
	}
	
	public void setTransitType( TransitType transitType ) {
		this.getStartEndNode().setTransitType( transitType );
	}

	public String getNameStart() {
		Path path = this.getPath();
		if (path == null) {
			return null;
		} else {
			Node node = path.getNodeStart();
			if (node == null) {
				return null;
			} else {
				return node.getName();
			}
		}
	}

	public String getNameEnd() {
		Path path = this.getPath();
		if (path == null) {
			return null;
		} else {
			Node node = path.getNodeEnd();
			if (node == null) {
				return null;
			} else {
				return node.getName();
			}
		}
	}

	public Path getPath() {
		return this.path;
	}

	public double getTime_Sec() {
		return this.time_Sec;
	}

	public double getCarbon_Gram() {
		return CarbonCalculator.getCarbonCalculator().getCarbon_Gram( this.getTransitType(), this.getLength_Meter() );
	}

	private double getCarbon_Gram_Old() {
		double length_Meter = this.getLength_Meter();
		double time_Sec = this.getTime_Sec();
		double cabonCoeff = this.getTransitType().getCarbonEmissonCoeff();
		double carbon_Gram = cabonCoeff * 1000 / (365 * 24 * 3600) * length_Meter * time_Sec;
		this.carbon_Gram = carbon_Gram;

		return this.carbon_Gram;
	}

	public double getLength_Meter() {
		return this.length_Meter;
	}

	public void setTime_Sec(double time_Sec) {
		this.time_Sec = time_Sec;
		this.getCarbon_Gram(); // 탄소 배출량을 재계산함. 시간 설정을 다시 하였을 때....
	}

	public void setLength_Meter(double length_Meter) {
		this.length_Meter = length_Meter;
		this.getCarbon_Gram(); // 탄소 배출량을 재계산함. 시간 설정을 다시 하였을 때....
	}

	private StartEndNode getStartEndNode() {
		return this.seNode;
	}

	// private String nameSection;
	private StartEndNode seNode;

	private double time_Sec;
	private double length_Meter;
	private double carbon_Gram;
	private double queryTime_Sec;

	private Path path;

}
